<?php
/*
 * Created on May 15, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
?>
<?php
/*
 * Created on May 15, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
?>
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 *  User_group Model Class
 *
 */
class  Unit_model extends CI_Model
{
	/**
	 * @var unknown_type
	 */
	var $id  ;  
	/**
	 * @var unknown_type
	 */
	var $category_parent_id  ;  
	/**
	 * @var unknown_type
	 */
	var $name_vi  ; 
	/**
	 * @var unknown_type
	 */
	var $name_en  ;  
	/**
	 * @var unknown_type
	 */
	var $description_vi;
	/**
	 * @var unknown_type
	 */
	var $description_en;
	/**
	 * @var unknown_type
	 */
	var $picture;
	/**
	 * @var unknown_type
	 */
	var $active;
	/*
	 * @access	public
	 */

        function __construct() {
            parent::__construct();
        }
        
	function Unit_model()
	{
		parent::__construct();
	}
	
	function create($categories){
		if(!is_array($categories))
			$categories = array($categories);
		
		$sql = "INSERT INTO `gtb_unit` " .
				"(" .
				"`vi_name`, `en_name`)" .
				"VALUES";
		for($i = 0; $i < count($categories); $i++){
			$category = $categories[$i];
			$sql .= "(" .
					 $this->db->escape($category->vi_name).", " .
					 $this->db->escape($category->en_name).", " .
					"),";
				
		}
		$sql = substr($sql, 0, strlen($sql)-1);
		$this->db->query($sql);
		return $this->db->affected_rows(); 
		
	}
	
	function insert($unit){
				
		$sql = "INSERT INTO `gtb_unit` " .
				"(" .
				"`vi_name`, `en_name`) " .
				" VALUES ";
		
		$sql .= "(" .
				$this->db->escape($unit->vi_name).", " .
				$this->db->escape($unit->en_name).
				")";
		
		$this->db->query($sql);
		return $this->db->insert_id();
		
	}
	
	function update($category){
		 $sql = "UPDATE gtb_pcategory 
				SET 
				code = " .  $this->db->escape($category->code) . ", " .
				"id_parent_pcategory = " .  intval($category->id_parent_pcategory) . "," .
				"code_parent = " .  $this->db->escape($category->code_parent) . ",". 
				"vi_name = " .  $this->db->escape($category->vi_name) . "," .
				"en_name = " .  $this->db->escape($category->vi_name) . ", " .
				"vi_description = " .  $this->db->escape($category->vi_description) . ", " .
				"en_description = " .  $this->db->escape($category->en_description) . ", " .
				"number_product = " .  intval($category->number_product) . ", " .
				"link_rewrite = " .  $this->db->escape($category->link_rewrite) . ", " .
				"meta_title = " .  $this->db->escape($category->meta_title) . ", " .
				"meta_keywords = " .  $this->db->escape($category->meta_keywords) . ", " .
				"meta_description = " .  $this->db->escape($category->meta_description) . ", " .
				"level_depth = " .  $this->db->escape($category->level_depth) . ", " .
				"active = " .  intval($category->active) . ", " .
				"date_upd = " .  $this->db->escape($category->date_upd) .
				"WHERE id_pcategory = " .intval($category->id_pcategory);
		$this->db->query($sql);
		return $this->db->affected_rows(); 
	}
	
	function delete($id){
		if(!is_array($id)) $id = array($id);
		$ids = implode(',', $id);
		$sql = "DELETE FROM gtb_pcategory WHERE id_pcategory IN ($ids)";
		//$sql = "DELETE FROM gtb_pcategory WHERE id_pcategory = " . intval($id);
		$this->db->query($sql);
		return $this->db->affected_rows(); 
	}
	
	function getAll(){
		$result = null;
		$sql = "SELECT u.* " .
				" FROM gtb_unit u " .
				" ORDER BY u.id DESC";
		
		$query = $this->db->query($sql);
		$result = $query->result();
       	
		return $result;
	}
	
	function getByParentCode($code_parent){
		$result = null;
		$sql = "SELECT *
				FROM gtb_pcategory WHERE code_parent_pcategory = '".$this->db->escape($code_parent)."' ORDER BY id_pcategory DESC";
		
		$query = $this->db->query($sql);
		$result = $query->result();
       	
		return $result;
	}
	
	function getMaxPosition($category_parent_id){
		
		$sql = "SELECT MAX(position) as position_max
				FROM category 
				WHERE category_parent_id = $category_parent_id";
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0]->position_max;
		}
       
		return 0;
	}
	
	function getById($category_id){
		
		$sql = "SELECT *
				FROM gtb_unit 
				WHERE id = " . intval($category_id);
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0];
		}
       
		return null;
	}
	
	function getByCode($code){
		
		$sql = "SELECT *
				FROM gtb_pcategory 
				WHERE code = " . $this->db->escape($code);
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0];
		}
       
		return null;
	}
	
	function getByLinkRewrite($link_rewrite){
		
		$sql = "SELECT *
				FROM gtb_pcategory 
				WHERE link_rewrite = " . $this->db->escape($link_rewrite);
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0];
		}
       
		return null;
	}
	
	function isInGroup($user_id, $group_id){
		$sql = "SELECT 1 as is_member FROM user_group ug
				INNER JOIN users u ON u.id = ug.user_id
				WHERE group_id = $group_id AND ug.user_id = $user_id";
		$query = $this->db->query($sql);
		if($query->result())
			return 1;
		
		return 0;
		
	}
	
	//function getUserBy
	
//	
}

// EOF

